//
//  shiweilistViewController.m
//  tongxiangOASystem
//
//  Created by michael on 2018/4/3.
//  Copyright © 2018年 czd. All rights reserved.
//
#import "HJDropDownMenu.h"
#import "shiweiViewController.h"
#import "shiweilistViewController.h"

@interface shiweilistViewController ()
@property (nonatomic, assign) NSInteger year;
@property (nonatomic, strong) NSString* yearstring;

@property (nonatomic, strong) NSArray *yearArray;
@property (nonatomic, strong) HJDropDownMenu * yearMenu;
@end

@implementation shiweilistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
           self.title = @"市委文件";

        self.view.backgroundColor = [UIColor whiteColor];

     NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute fromDate:[NSDate date]];
    _year = [components year];
    _yearstring = [NSString stringWithFormat:@"%ld",(long)_year];
// NSInteger firstYear = 2018;
    NSMutableArray *yearArray = [NSMutableArray array];
    for (int i = 0; i <= _year-2000; i++) {
        [yearArray addObject:[NSString stringWithFormat:@"%ld", _year - i]];
    }
    _yearArray = yearArray;
    
     _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, KScreenWidth,KScreenHeight-kTopHeight-50-LL_TabbarSafeBottomMargin) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//        self.tableView.backgroundColor = [UIColor colorWithHexString:@"efeff4"];
        self.tableView.backgroundColor = [UIColor whiteColor];
_tableView.bounces = NO;
_tableView.dataSource = self;
_tableView.delegate = self;
[self.view addSubview:_tableView];
  _yearMenu = [[HJDropDownMenu alloc] initWithFrame:CGRectMake(10, 5, KScreenWidth-20, 40)];
    _yearMenu.rowHeight = 40;
    _yearMenu.datas = _yearArray;
    [self.view addSubview:_yearMenu];
    [_yearMenu setCellClickedBlock:^(NSString *title, NSInteger index) {
_yearstring = title;
[_yearMenu closeMenu];
}];
              [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    [self loaddata];

    // Do any additional setup after loading the view.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[_yearMenu closeMenu];
}
- (void)loaddata{
 NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"userId":userId}];
[HttpTools requestWithURL:@"exchgdotwo" arrays:array method:@"getSwGwFstword" success:^(id JSON) {
    NSLog(@"%@",JSON);
              [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

  
      _dataArray =   [NSMutableArray array];
        NSArray *array = JSON[@"data"];
 
         for (NSDictionary *dic in array) {
    NSDictionary *dict = dic;
    [_dataArray addObject:dict];
    
}
[self initview];

        [self.tableView reloadData];

 
        if (self.dataArray.count ==0) {
            [self.view makeToast:@"暂时还没数据哦"];
        }
    } failure:^(NSError *error) {
              [MBProgressHUD hideAllHUDsForView:self.view animated:YES];

        
    }];
}
- (void)initview{
if (!_tableView) {
    
}

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
  return  44;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

 static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;//cell的右边有一个小箭头，距离右边有十几像素；

    }
    cell.imageView.image = [UIImage imageNamed:@"蓝"];
 NSDictionary *dic = self.dataArray[indexPath.row];
     cell.backgroundColor = [UIColor whiteColor];
    cell.textLabel.text =dic[@"GwFstword"];
    return cell;


 }
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[_yearMenu closeMenu];
        NSDictionary *dic = self.dataArray[indexPath.row];
      UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//     detailViewController * vc = (detailViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"detail"];
     shiweiViewController *vc = [[shiweiViewController alloc]init];
vc.year = _yearstring;
vc.GwFstword = dic[@"GwFstword"];
            vc.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:vc animated:YES];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
